//go:generate fyne bundle -o bundled.go Icon.png

package main

import (
	"os"
	"strconv"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

const winTitle = "Fyles"

// 在底部状态栏显示信息
var msg binding.String = binding.NewString()
var bl binding.StringList //收藏夹路径列表
var ui, currentUI *fyles
var buri binding.URI = binding.NewURI() //当前目录路径

func main() {
	/* 	if runtime.GOOS == "windows" {
		getStorageInfo()
	} */
	a := app.NewWithID("io.fyne.fyles")
	a.SetIcon(resourceIconPng)
	a.Settings().SetTheme(&myTheme{})
	w := a.NewWindow(winTitle)
	w.SetMaster()
	fileItemMin = fyne.NewSize(fileIconCellWidth, fileIconSize+fileTextSize+theme.Padding())

	path, _ := os.Getwd()
	if len(os.Args) > 1 {
		path = os.Args[1]
	}
	current := storage.NewFileURI(path)
	ui = &fyles{win: w, filter: filterHidden()}
	tools := ui.makeToolbar()
	ui.items = container.NewGridWrap(fileItemMin)
	ui.fileScroll = container.NewScroll(ui.items)
	currentUI = ui
	fileTree := ui.makeFilesPanel(storage.NewFileURI("/"))
	//fileTree := ui.makeFilesPanel(current)
	ui.setDirectory(current)
	ui2 := &fyles{win: w, filter: filterHidden()}
	ui2.items = container.NewGridWrap(fileItemMin)
	ui2.fileScroll = container.NewScroll(ui2.items)
	ui2.setDirectory(current)
	buri.AddListener(binding.NewDataListener(func() {
		msg.Set(strconv.Itoa(len(ui.items.Objects)-1) + " items")
		currentUI.setLocation(currentUI.pwd)
		// ui.setLocation(ui.pwd)
	}))
	lblInfo := widget.NewLabelWithData(msg)

	bodySplit := container.NewHSplit(ui.fileScroll, ui2.fileScroll)
	bodySplit.Offset = 0.5
	body := container.NewBorder(nil, lblInfo, nil, nil, bodySplit)
	// body := container.NewBorder(nil, lblInfo, nil, nil, ui.fileScroll)

	bl = binding.NewStringList()
	lifecycle(a, bl)
	homedir, _ := os.UserHomeDir()
	cachedir, _ := os.UserCacheDir()
	configdir, _ := os.UserConfigDir()
	tempdir := os.TempDir()
	bl.Append(homedir)
	bl.Append(cachedir)
	bl.Append(configdir)
	bl.Append(tempdir)
	// lst := LblList(ui.bl)
	// lst := RightClickLblList(ui.bl)
	lst := DeleteList(bl)
	lst.OnSelected = func(id widget.ListItemID) {
		pth, err := bl.GetValue(id)
		if err != nil {
			return
		}
		currentUI.setDirectory(storage.NewFileURI(pth))
		// ui.setDirectory(storage.NewFileURI(pth))
	}

	left := container.NewVSplit(fileTree, lst)

	left.SetOffset(0.5)
	mainSplit := container.NewHSplit(left, body)
	mainSplit.Offset = 0.35
	w.SetMainMenu(MainMenuu(a))
	w.SetContent(container.NewBorder(tools, nil, nil, nil, mainSplit))
	w.Resize(fyne.NewSize(800, 700))
	w.ShowAndRun()
}

// 构造工具栏
func makeToolbar(ui *fyles) *fyne.Container {
	// l := widget.NewLabel("")
	// ui.filePath = l
	ui.filePath = container.NewHBox()
	var toggleViewButton *widget.Button
	toggleViewButton = widget.NewButtonWithIcon("", theme.ListIcon(), func() {
		if ui.view == gridView {
			ui.setView(listView)
			toggleViewButton.SetIcon(theme.GridIcon())
		} else {
			ui.setView(gridView)
			toggleViewButton.SetIcon(theme.ListIcon())
		}
	})

	return container.NewBorder(nil, nil, widget.NewToolbar(
		widget.NewToolbarAction(theme.MoveUpIcon(), func() {
			p, err := storage.Parent(ui.pwd)
			if err != nil {
				return
			}
			ui.setDirectory(p)
		}),
		widget.NewToolbarAction(theme.FolderNewIcon(), func() {
			dialog.ShowEntryDialog("New Folder", "Name", func(s string) {
				if s == "" {
					return
				}
				os.Mkdir(s, 0777)
				ui.items.Add(newFileItem(storage.NewFileURI(s), true, ui))
				msg.Set("Create folder :" + s)
			}, ui.win)
		}),
		widget.NewToolbarAction(theme.HomeIcon(), func() {
			home, err := os.UserHomeDir()
			if err != nil {
				return
			}
			ui.setDirectory(storage.NewFileURI(home))
		}),
	), toggleViewButton,
		container.NewHScroll(ui.filePath))
	// container.NewHScroll(l))
}

func lifecycle(a fyne.App, bl binding.StringList) {
	a.Lifecycle().SetOnStopped(func() {
		bls, _ := bl.Get()
		a.Preferences().SetString("bl", strings.Join(bls, ","))
	})
	a.Lifecycle().SetOnStarted(func() {
		homedir, _ := os.UserHomeDir()
		ss := a.Preferences().StringWithFallback("bl", homedir)
		sl := strings.Split(ss, ",")
		bls, _ := bl.Get()
		for _, v := range sl {
			if !IsInSlice(v, bls) {
				bl.Append(v)
			}
		}
	})
}
